package com.dahuyou.netty.reconnect.client;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;

import java.util.concurrent.TimeUnit;

public class MyChannelFutureListener implements ChannelFutureListener {
    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        // 不success说明断开了,就重连啦！！！
        if (channelFuture.isSuccess()) {
            System.out.println("netty client start done. {}");
            return;
        }
        final EventLoop loop = channelFuture.channel().eventLoop();
        loop.schedule(new Runnable() {
            @Override
            public void run() {
                try {
                    // 老的就不要了，直接搞一个新的就行！！！
                    new NettyClient().connect("127.0.0.1", 7397);
                    System.out.println("netty client start done. {}");
                    Thread.sleep(500);
                } catch (Exception e){
                    System.out.println("netty client start error go reconnect ... {}");
                }
            }
        }, 1L, TimeUnit.SECONDS);
    }
}
